package com.shengjing.apk.urlprocessor

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.webkit.WebView

/**
 * Created by sfshine@qq.com on 2019/3/17 0017.
 */
object UrlProcessor {
    private const val TAG: String = "UrlProcessor"

    fun process(webview: WebView, url: String) {
        when {
            url.startsWith("tel:") -> dial(webview, url)
            url.startsWith("sms:") -> sms(webview, url)
            url.startsWith("objc:") -> OBJCProcessor.process(webview, url)
            else -> webview.loadUrl(url)
        }
    }

    private fun dial(webview: WebView, phone: String) {
        webview.reload()
        Log.i(TAG, "dial:  $phone")
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse(phone))
        webview.context.startActivity(intent)
    }


    fun sms(webview: WebView, sms: String) {
        webview.reload()
        Log.i(TAG, "sending:  $sms")
        val uri = Uri.parse(sms)
        val intent = Intent(Intent.ACTION_SENDTO, uri)
        webview.context.startActivity(intent)
    }
}